package com.wing.cabinet.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.cabinet.dao.UseInventoryUserDao;
import com.wing.cabinet.model.entity.UseInventoryUser;
import com.wing.cabinet.model.request.UseInventoryUserForm;
import com.wing.cabinet.model.response.KnifeUserVO;
import com.wing.cabinet.model.response.UseInventoryUserVO;
import com.wing.common.utils.JsonResult;
import com.wing.user.feign.UserClient;
import com.wing.user.model.response.DepartmentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 用户产品在用库存-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class UseInventoryUserServiceImpl extends ServiceImpl<UseInventoryUserDao, UseInventoryUser> implements UseInventoryUserService {

    @Autowired
    private UserClient userClient;
    @Override
    public int add(UseInventoryUserForm useInventoryUserForm) {

        //校验

        UseInventoryUser useInventoryUser =new UseInventoryUser();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(UseInventoryUser.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(useInventoryUserForm, useInventoryUser, copyOptions);

        return baseMapper.insert(useInventoryUser);
    }

    @Override
    public List<UseInventoryUserVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<UseInventoryUserVO> pageList4app(Page page, Map<String, Object> params) {
        List<UseInventoryUserVO> list = baseMapper.list4app(page, params);
        if (params.get("labelCode")!=null&&!"".equals(params.get("labelCode").toString())&&list.size()>0){
            list.get(0).setQuantity(1);
        }
        for(UseInventoryUserVO item : list){
            JsonResult<DepartmentVO> jsonResult = userClient.getDepartmentVOById(item.getDepartmentId());
            if(null != jsonResult && null != jsonResult.getData()) {
                item.setCostCenter(jsonResult.getData().getCostCenter());
            }
            JsonResult<DepartmentVO> jsonResult2 = userClient.getDepartmentVOById(item.getDeviceDepartmentId());
            if(null != jsonResult2 && null != jsonResult2.getData()) {
                item.setDeviceDepartmentName(jsonResult2.getData().getName());
            }
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public UseInventoryUserVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public List<UseInventoryUserVO> ecxportList(Map<String, Object> params) {
        List<UseInventoryUserVO> list = baseMapper.list4app(params);
        for(UseInventoryUserVO item : list){
            JsonResult<DepartmentVO> jsonResult = userClient.getDepartmentVOById(item.getDepartmentId());
            if(null != jsonResult && null != jsonResult.getData()) {
                item.setCostCenter(jsonResult.getData().getCostCenter());
            }
            JsonResult<DepartmentVO> jsonResult2 = userClient.getDepartmentVOById(item.getDeviceDepartmentId());
            if(null != jsonResult2 && null != jsonResult2.getData()) {
                item.setDeviceDepartmentName(jsonResult2.getData().getName());
            }
        }
        /*try {
            for (int i = 0; i < list.size(); i++) {
                String skuPic = list.get(i).getSkuPic();

                if (StringUtils.isNotEmpty(skuPic)) {
                    URL pic = new URL(skuPic);
                    list.get(i).setSkuPicVo(pic);
                }
            }
        } catch (MalformedURLException e) {
            System.out.println("在用统计URL转换失败：" + e.getMessage());
        }*/

        return list;
    }


    @Override
    public List<KnifeUserVO> getKnifeUsers() {
        return baseMapper.getKnifeUsers();
    }
}
